package org.jtools.sanitizer.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.jtools.sanitizer.Sanitizer;

public class StringSanitizer implements Sanitizer {

    private static final List<String> patternList = new ArrayList<String>();

    public StringSanitizer(String... patterns) {
        patternList.addAll(Arrays.asList(patterns));
    }

    public Object sanitize(Object obj) {
        if (!(obj instanceof Throwable))
            return doSanitize(obj.toString());

        return obj;
    }

    public String sanitize(String str) {
        return doSanitize(str);
    }

    protected String doSanitize(String str) {
        for (String pattern : patternList)
            str = str.replaceAll(pattern, "****");

        return str;
    }
}
